package mapEditor.utils;

import mapEditor.map.MapGrid;

import java.awt.GridLayout;
import java.awt.geom.Point2D;

import javax.swing.JLabel;
import javax.swing.JPanel;

public class MapLocation extends ValueType
{ private MapGrid map;
//  private double EAST;  // x-coordinate in meter from ??? (London ?)
//  private double NORTH; // y-coordinate in meter from ??? (Ekvator ?)
  private double x; // x-coordinate in pixels relative map original dimension (not zoomed)
  private double y; // y-coordinate in pixels relative map original dimension (not zoomed)

  private MapLocation(MapGrid map) { this.map=map; }

  //public Location(double east,double north)
  //{ setEast(east); setNorth(north); }
  
  public static MapLocation newPixelPoint(MapGrid map,double x,double y) // x,y pixels at current screen
  { MapLocation location=new MapLocation(map);
    location.setX(x); location.setY(y);
    return(location);	
  }
  
  public MapLocation copy()
  { MapLocation location=new MapLocation(map);
    location.x=x; location.y=y;
    return(location);	
  }

  public double getX() { return( x*map.getCurrentZoom()); }
  public double getY() { return( y*map.getCurrentZoom()); }
  public double getMapPixelX() { return(x); } // Raw MapImage x-pixel
  public double getMapPixelY() { return(y); } // Raw MapImage x-pixel
  public void setX(double x) { double newX=x/map.getCurrentZoom(); if(newX!=this.x) { this.x=newX; FIRE_CHANGE_EVENT("X"); } }
  public void setY(double y) { double newY=y/map.getCurrentZoom(); if(newY!=this.y) { this.y=newY; FIRE_CHANGE_EVENT("Y"); } }

  public double getEast()
  { double xMeterFromOrigo=map.xPixelInMeter()*(getX()-map.getGridOrigoX());
    double east=map.getGridOrigoEast()+xMeterFromOrigo;
    east=Util.round(east,10000);
    return(east);  		
  }
  
  public double getNorth()
  { double yMeterFromOrigo=map.yPixelInMeter()*(getY()-map.getGridOrigoY());
    double north=map.getGridOrigoNorth()-yMeterFromOrigo;
    north=Util.round(north,10000);
    return(north);  		
  }
  
  public void setEast(double newEast)
  { double xPix=map.xMetersToPixel()*(newEast-map.getGridOrigoEast());
    xPix=Util.round(xPix,100);
    setX(map.getGridOrigoX()+xPix);
  }
  public void setNorth(double newNorth)
  { double yPix=map.yMetersToPixel()*(map.getGridOrigoNorth()-getNorth());
    yPix=Util.round(yPix,100);
    setY(map.getGridOrigoY()+yPix);
  }

  // ******************************************************************
  // *** METHOD: getDistanceInMeter
  // ******************************************************************
  public double getDistanceInMeter(MapLocation other)
  { Point2D p1=new Point2D.Double(this.getEast(),this.getNorth());
    Point2D p2=new Point2D.Double(other.getEast(),other.getNorth());
    return(p1.distance(p2));
  }

  // ****************************************************************
  // *** METHOD: getSpinnerPanel
  // ****************************************************************
  public JPanel getSpinnerPanel()
  { final double MINIMUM=0;
    final double MAXIMUM=Integer.MAX_VALUE;
    JPanel panel=new JPanel();
    panel.setLayout(new GridLayout(2,2));
    double stepSize=0.01; // 1 cm
    panel.add(new JLabel("  X:   UTM32 EAST"));
    panel.add(new ValueSpinner(getEast(),MINIMUM,MAXIMUM,stepSize)
    { static final long serialVersionUID=0; // AD'HOC
      public void update(double value) { setEast(value); }
    });
    panel.add(new JLabel("  Y:   UTM32 NORTH"));
    panel.add(new ValueSpinner(getNorth(),MINIMUM,MAXIMUM,stepSize)
    { static final long serialVersionUID=0; // AD'HOC
      public void update(double value) { setNorth(value); }
    });
    return(panel);
  } 

  // ******************************************************************
  // *** METHOD: toString
  // ******************************************************************
  public String toString()
  { return("MapLocation[east="+getEast()+",north="+getNorth()+"]"); }
}
